home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*_________________________________________________________________________
- |
- | io.c - file opening, parsing and closing routines
- |
- | only one file can be open at any time; no check is made
- |
- | (c) 1993 Frans van Hoesel, Xtreme graphics software
- |
- */
-
-
-
- #include <ctype.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <string.h>
- #include <bstring.h>
- #include <ctype.h>
- #include <stdarg.h>
- #include <gl/gl.h>
-
- #include "config.h"
- #include "blixsound.h"
- #include "blix.h"
- #include "io.h"
-
- /*_________________________________________________________________________
- |
- | static variables and constants used while reading the data files
- |
- */
-
-
- #define MAXLEN 256
- #ifndef UNCOMPRESS
- #define UNCOMPRESS "/usr/bsd/uncompress"
- #endif
-
-
- /* static variables */
- static char text[MAXLEN + 2];
- static char *chptr;
- static FILE *this_file = NULL;
- static char *filename;
- static int linenum;
- static int piping;
-
- void errormsg(const char *format, ...) {
- va_list args;
-
- /* CONSTANTCONDITION */
- va_start(args, format);
- if (linenum == 0) {
- fprintf(stderr, "%s: error: ", basename);
- } else {
- fprintf(stderr, "%s: error in file '%s' line %d: ", basename,
- filename, linenum);
- }
- vfprintf(stderr, format, args);
- fprintf(stderr, "\n");
- va_end(args);
- end_sound();
- gexit();
- exit (1);
- }
-
- void warningmsg(const char *format, ...) {
- va_list args;
-
- /* CONSTANTCONDITION */
- va_start(args, format);
- if (linenum == 0) {
- fprintf(stderr, "%s: Warning: ", basename);
- } else {
- fprintf(stderr, "%s: Warning from file '%s' line %d: ", basename,
- filename, linenum);
- }
- vfprintf(stderr, format, args);
- fprintf(stderr, "\n");
- va_end(args);
- }
-
- char *lookahead(void) {
- return chptr;
- }
- void open_file(const char *file_name) {
-
- char fullpath[MAXLEN * 2];
- char pipecmd[MAXLEN * 2];
- piping = FALSE;
- linenum = 0;
- filename = (char *) file_name;
- strcpy(fullpath, datadir);
- strcat(fullpath, filename);
-
- if (this_file != NULL) {
- close_file();
- }
- this_file = fopen(fullpath,"r");
-
- if (this_file == NULL) {
- /* perhaps the file is compressed ? */
- strcat(fullpath, ".Z");
- this_file = fopen(fullpath,"r");
- if (this_file != NULL) {
- fclose(this_file);
- strcpy(pipecmd, UNCOMPRESS);
- strcat(pipecmd, " -c ");
- strcat(pipecmd, fullpath);
- this_file = popen(pipecmd, "r");
- if (this_file != NULL)
- piping = TRUE;
- }
- }
- if (this_file == NULL) {
- errormsg("cannot open file '%s'", filename);
- }
- read_line(text);
- chptr = text;
- }
-
- void close_file(void) {
-
- if (piping) {
- if (pclose(this_file)) {
- errormsg("error while reading compressed data");
- }
- } else {
- fclose(this_file);
- }
- this_file = NULL;
- }
-
- int read_line(char *l) {
- char *r;
-
- do {
- r = fgets(l, MAXLEN+1, this_file);
- linenum++;
- } while (r != NULL && (*l == '\0' || *l == '#')) ;
-
- return (r != NULL);
- }
-
- #pragma inline
- int skipblanks(void) {
- loop:
- while (*chptr == ' ' || *chptr == '\t' || *chptr == '\n')
- chptr++;
- if (*chptr == '\0' || *chptr == '#') {
- /* time to read the next line */
- if (!read_line(text)) {
- chptr = "end";
- return *chptr;
- }
- chptr = text;
- goto loop;
- }
- return *chptr;
- }
-
- #define skipblanks_macro() \
- while (*chptr == ' ' || *chptr == '\t' || *chptr == '\n') { \
- chptr++; \
- } \
- while (*chptr == '\0' || *chptr == '#') { \
- if (!read_line(text)) { \
- chptr = "end"; \
- } else {\
- chptr = text; \
- while (*chptr == ' ' || *chptr == '\t' ) { \
- chptr++; \
- } \
- } \
- } \
-
-
- void expect_ch(const char c) {
- skipblanks_macro();
- if ( *chptr == c)
- chptr++;
- else
- errormsg("'%c' expected", c);
- }
-
- int test_ch(const char c) {
- skipblanks_macro();
- return (*chptr == c);
- }
-
- int test_str(const char *s) {
- skipblanks_macro();
- if (*chptr == *s) {
- return (strncmp (s, chptr, strlen(s)) == 0);
- } else {
- return 0;
- }
- }
-
- int read_str(const char *s) {
- if (test_str(s)) {
- chptr += strlen(s);
- return 0;
- } else {
- return 1;
- }
- }
-
- int read_ch(const char c) {
- skipblanks_macro();
- if (*chptr == c) {
- chptr++;
- return 0;
- } else {
- return 1;
- }
-
- }
- float accept_float(void) {
-
- char *ptr;
- float f;
-
- skipblanks_macro();
- f = strtod(chptr, &ptr);
- if (chptr == ptr)
- errormsg("floating point number expected (%s)", chptr);
- else
- chptr = ptr;
- return f;
- }
-
-
- int accept_int(void) {
-
- int s = 1;
- int n;
-
- skipblanks_macro();
- if (*chptr == '-') {
- s = -1;
- chptr++;
- }
- if (! isdigit(*chptr))
- errormsg("digit expected");
- n = *chptr++ - '0';
- while (isdigit (*chptr)) {
- n = n * 10 + *chptr - '0';
- chptr++;
- }
- return (s * n);
- }
-
- char *accept_str(void) {
- static char st[MAXLEN];
- char *s;
-
- s = st;
- skipblanks_macro();
- while (isalnum(*chptr)) {
- *s++ = *chptr++;
- }
- *s = '\0';
- return st;
- }
-
- void skip_line(void) {
- if (!read_line(text)) {
- chptr = "end";
- } else {
- chptr = text;
- }
- }
-